package com.yowasa.xiuxian.engine.message.cmd;

import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.BaseMessageHandler;
import com.yowasa.xiuxian.entity.bot.message.MessageKeyboard;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.util.ContentUtil;
import com.yowasa.xiuxian.util.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@CMD(name = "/指令",outCmd = true)
public class GetPrivateCmd implements BaseMessageHandler {
//    @Autowired
//    private QQChannelService qqChannelService;

    public static final Logger logger = LoggerFactory.getLogger(GetPrivateCmd.class);
    @Autowired
    private MessageHelper messageHelper;

    @Autowired
    private List<BaseMessageHandler> messageHandlers;

//    @Override
//    public boolean match(Message message) {
//        if (message.isDirectMessage()) return false;
//        String noUserContent = ContentUtil.getNoUserContent(message.getContent());
//        if (noUserContent.startsWith("/指令")) {
//            return true;
//        }
//        return false;
//    }

    @Override
    public boolean match(String message) {
        String noUserContent = ContentUtil.getNoUserContent(message);
        if (noUserContent.startsWith("/指令")) {
            return true;
        }
        return false;
    }

//        List<UserItemBo> list = itemHelper.list(user.getId());
//        messageHelper.reply(message, "持有摩拉:" + user.getUserConfig().getGold() + "\n" + "持有道具:\n"
//                + list.stream().map(e -> buildItemListDesc(e)).collect(Collectors.joining("\n")));
//        return;
//    }

    @Override
    public void execute(SendMessage message) {
        List<String> list = new ArrayList<>();
        list.add("指令名称     作用范围    能否在地图外使用\n");
        for (BaseMessageHandler messageHandler : messageHandlers) {
            CMD annotation = messageHandler.getClass().getAnnotation(CMD.class);
            if(annotation != null){
                list.add(annotation.name()+"        "+annotation.scope()+"       "+annotation.outCmd());
            }else {
                logger.info("当前执行类无法获取CMD注解：{}",messageHandler);
            }

        }
        messageHelper.replyMys(message,String.join("\n",list));

//        SendMessageReq sendMessageReq = new SendMessageReq();
//
//        MessageMarkdown mdMessage = new MessageMarkdown();
//        mdMessage.setContent("指令列表");
//        sendMessageReq.setMarkdown(mdMessage);
//        sendMessageReq.setMsgId(message.getId());
////        sendMessageReq.setKeyboard(build());
//        qqChannelService.originSend(message.getChannelId(), sendMessageReq);

    }

    private MessageKeyboard build() {
        String button = "{\"content\":[{\"rows\":[{\"buttons\":[{\"id\":\"1\",\"render_data\":{\"label\":\"button\",\"visited_label\":\"Button\"},\"action\":{\"type\":1,\"permission\":{\"type\":2,\"specify_role_ids\":[\"1\"]},\"click_limit\":10,\"data\":\"搜索\",\"at_bot_show_channel_list\":true}}]}]}]}";
        MessageKeyboard keyboard = JSONUtils.toObject(button, MessageKeyboard.class);

        return keyboard;
    }
    public String desc(){
        return new StringBuffer().append("查看所有指令名称的指令")
                .toString();

    }

}
